#include <QStandardItemModel>
#include "CaptureDateList.h"
#include "ui_CapturedateList.h"

CaptureDateList::CaptureDateList(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::CaptureDateList)
{
	ui->setupUi(this);

	connect(ui->tableView, SIGNAL(clicked(QModelIndex)), SLOT(SlotSelectedPatient(QModelIndex)));
}

CaptureDateList::~CaptureDateList()
{
	delete ui;
}

void CaptureDateList::setCaptureDateList(const QList<SCapturePerDate> &listCapture)
{
	QStandardItemModel *pModel = new QStandardItemModel;

	foreach (const SCapturePerDate &capture, listCapture)
	{
		QListIterator<int> li(capture.m_listPosition);
		QStringList listStr;

		while (li.hasNext())
		{
			QString strPos = QString::number(li.next());

			if(listStr.contains(strPos)) continue;
			else listStr << strPos;

		}

		QStandardItem *itemID = new QStandardItem;

		itemID->setData(capture.m_iPatientID, Qt::DisplayRole);
		pModel->appendRow(QList<QStandardItem*>()
						  << itemID
						  << new QStandardItem(capture.m_timeCapture.toString(strDateFormat))
						  << new QStandardItem(listStr.join(' ')));
	}

	pModel->setHeaderData(1, Qt::Horizontal, "촬영일");
	pModel->setHeaderData(2, Qt::Horizontal, "촬영치아");
	ui->tableView->setModel(pModel);
	ui->tableView->horizontalHeader()->setSectionHidden(0, true); //id 컬럼 숨김
	ui->tableView->resizeColumnsToContents();
	ui->tableView->horizontalHeader()->setStretchLastSection(true);
}

void CaptureDateList::SlotSelectedPatient(const QModelIndex &index)
{
	QDate dateCapture = QDate::fromString(index.sibling(index.row(), 1).data().toString(), strDateFormat);

	emit SignalSelectedCaptureDate(dateCapture);
}
